package com.wjerp.tplus.server.common.utils;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.wjerp.tplus.server.api.request.AddFeedbackRequest;
import com.wjerp.tplus.server.api.request.AddInvoiceInfoRequest;
import com.wjerp.tplus.server.common.constant.RedisKey;
import com.wjerp.tplus.server.common.exception.ServiceException;
import com.wjerp.tplus.server.common.model.BaseResponseData;
import com.wjerp.tplus.server.domain.entity.CompanyAccountInfo;
import com.wjerp.tplus.server.domain.entity.CompanyInfo;
import com.wjerp.tplus.server.domain.entity.invoice.mng.InvoiceInfo;
import com.wjerp.tplus.server.domain.mapper.CompanyInfoMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 发票查验操作集成类
 * @author lishuailei
 */
@Component
public class WjOpenApiUtils {

    /** The logger. */
    public static Logger logger = LoggerFactory.getLogger(WjOpenApiUtils.class);

    @Value("${wj.openApi.checkUrl}")
    private String checkUrl;

    @Value("${wj.openApi.refreshAccount}")
    private String refreshAccountUrl;

    @Value("${wj.openApi.newFeedbackUrl}")
    private String newFeedbackUrl;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private CompanyInfoMapper companyInfoMapper;

    /**
     * 刷新企业账户
     * @param companyCode
     * @return
     * @throws ServiceException
     */
    public CompanyAccountInfo refreshAccount(String companyCode) throws ServiceException {

        String result = HttpRequest.get(refreshAccountUrl)
                .header("authCode", getAuthCode(companyCode))
                .execute().body();
        BaseResponseData baseResponseData = JSON.parseObject(result, BaseResponseData.class);
        if(!baseResponseData.isSuccess()){
            throw new ServiceException(baseResponseData.getErrorCode(),baseResponseData.getErrorMsg());
        }
        CompanyAccountInfo companyAccountInfo = JSON.parseObject(JSON.toJSONString(baseResponseData.getData()),CompanyAccountInfo.class);
        return companyAccountInfo;
    }

    /**
     * 新增反馈信息
     * @param request
     * @param companyCode
     */
    public void newFeedback(AddFeedbackRequest request, String companyCode) throws ServiceException {
        String result = HttpRequest.post(newFeedbackUrl)
                .header("authCode", getAuthCode(companyCode))
                .body(JSON.toJSONString(request))
                .execute().body();
        BaseResponseData baseResponseData = JSON.parseObject(result, BaseResponseData.class);
        if(!baseResponseData.isSuccess()){
            throw new ServiceException(baseResponseData.getErrorCode(),baseResponseData.getErrorMsg());
        }
    }

    /**
     * 发票验真
     * @param invoiceInfo 发票信息
     * @param companyCode 公司编码
     * @return 查询结果
     */
    public InvoiceInfo verInvoice(InvoiceInfo invoiceInfo,String companyCode) throws  ServiceException {
        AddInvoiceInfoRequest addInvoiceInfoRequest = new AddInvoiceInfoRequest();
        BeanUtils.copyProperties(invoiceInfo,addInvoiceInfoRequest);
        String result = HttpRequest.post(checkUrl)
                .header("authCode", getAuthCode(companyCode))
                .body(JSON.toJSONString(addInvoiceInfoRequest))
                .execute().body();
        logger.info("verInvoice-request{},verInvoice-result{}",addInvoiceInfoRequest,result);
        BaseResponseData baseResponseData = JSON.parseObject(result, BaseResponseData.class);
        if(!baseResponseData.isSuccess()){
            throw new ServiceException(baseResponseData.getErrorCode(),baseResponseData.getErrorMsg());
        }

        InvoiceInfo invoiceInfo2 = JSON.parseObject(JSON.toJSONString(baseResponseData.getData()), InvoiceInfo.class);
        return invoiceInfo2;
    }

    /**
     * 获取企业授权码
     * @param companyCode
     * @return
     */
    public String getAuthCode(String companyCode){
        if(redisUtils.exists(RedisKey.AUTH_CODE)){
            return redisUtils.get(RedisKey.AUTH_CODE).toString();
        }

        //获取系统中的公司信息
        CompanyInfo companyInfo = companyInfoMapper.selectByCompanyCode(companyCode);
        redisUtils.set(RedisKey.AUTH_CODE,companyInfo.getAuthCode());
        return companyInfo.getAuthCode();
    }


}
